package com.hezhiqin.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: homeWork
 * @description:权限
 * @author: hezhiqin
 * @create: 2020-01-19 14:44
 */
public class CuratorAclDemo {

    public  static  final  String CONNECTION_STR ="192.168.223.128:2181,192.168.223.129:2181,192.168.223.130:2181";
    public static void main(String[] args) throws Exception {
        CuratorFramework framework = CuratorFrameworkFactory.builder()
                .connectString(CONNECTION_STR)
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3)).build();
        framework.start();

        List<ACL> list=new ArrayList<ACL>();

        ACL acl=new ACL(ZooDefs.Perms.READ | ZooDefs.Perms.WRITE,
                new Id("digest", DigestAuthenticationProvider.generateDigest("admin:admin")));
        list.add(acl);
        framework.create().withMode(CreateMode.PERSISTENT).withACL(list).forPath("/auth");

    }
}
